package com.miaomiaojia.android;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.miaomiaojia.bll.CommonInterface;
import com.miaomiaojia.bll.CommonTool;
import com.miaomiaojia.entity.StaticData;
import com.miaomiaojia.waterfall.WaterFallOption;
import com.miaomiaojia.waterfall.WaterFallView;
import com.miaomiaojia.waterfall.WaterFallView.OnScrollListener;
import com.miaomiaojia.waterfallitem.FlowView;
import com.miaomiaojia.waterfallitem.FlowViewHandler;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.view.Display;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TabHost;

public class MyActivity extends Activity implements OnScrollListener {

	private WaterFallView waterfall_scroll;
	private LinearLayout waterfall_container;
	private Display display;
	private AssetManager asset_manager;
	private List<String> image_filenames;
	private final String image_path = "images";
	private Handler handler;
	private int item_width;
	private int column_count = 3;
	private int classId = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (StaticData.IsLogin()) {
			getMyData();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!StaticData.IsLogin()) {
			Intent it = new Intent(this, LoginActivity.class);
			startActivityForResult(it,
					CommonInterface.RequestCode.REQUEST_LOGIN);
		}
		if (StaticData.MyIsNeedFresh) {
			getMyData();
			StaticData.MyIsNeedFresh = false;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case CommonInterface.RequestCode.REQUEST_LOGIN:
			if (resultCode == CommonInterface.LoginResult.LOGIN_OK) {
				getMyData();
			} else {
				Activity parent = (Activity) this.getParent();
				int id = R.id.tab_button_index;
				if (StaticData.PreTab == 1) {
					id = R.id.tab_button_list;
				} else if (StaticData.PreTab == 3) {
					id = R.id.tab_button_search;
				}
				// TabHost mTabHost = parent.getTabHost();
				// mTabHost.setCurrentTab(StaticData.PreTab);
				CompoundButton buttonView = (RadioButton) parent
						.findViewById(id);
				// buttonView.setBackgroundResource(R.drawable.style_tab_select);
				buttonView.setChecked(true);
			}
			break;

		default:
			break;
		}
	}

	private void getMyData() {
		setContentView(R.layout.activity_my);
		display = getWindowManager().getDefaultDisplay();
		// item_width = display.getWidth() / column_count;
		column_count = display.getWidth() / item_width;
		asset_manager = getAssets();

		initLayout();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_my, menu);
		return true;
	}

	private void initLayout() {

		waterfall_scroll = (WaterFallView) findViewById(R.id.waterfall_scroll);
		waterfall_container = (LinearLayout) findViewById(R.id.waterfall_container);
		waterfall_scroll.setOnScrollListener(this);
		WaterFallOption fallOption = new WaterFallOption(waterfall_container,
				item_width, column_count, display.getWidth());
		waterfall_scroll.commitWaterFall(fallOption, waterfall_scroll);
		handler = new FlowViewHandler(waterfall_scroll);
		AddItemToContainer(waterfall_scroll.current_page,
				waterfall_scroll.pageCount);
	}

	private void AddItemToContainer(final int pageindex, final int pagecount) {
		new Thread() {
			@Override
			public void run() {
				String url = getResources().getString(R.string.host)
						+ getResources().getString(R.string.fav);
				url = String.format(url, StaticData.User.getUserId(),
						pageindex + 1);
				String result = CommonTool.getResult(url);
				JSONObject data;
				try {
					data = new JSONObject(result);
					int status = data.getInt("status");
					if (status == 1) {
						JSONArray array = new JSONArray(data.getString("data"));
						for (int i = 0; i < array.length(); i++) {
							JSONObject produce = array.getJSONObject(i);
							String imgurl = produce.getString("simg");
							AddImage(imgurl,
									(int) Math
											.ceil(waterfall_scroll.loaded_count
													/ (double) column_count),
									produce.getInt("id"));
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();

	}

	private void AddImage(String filename, int rowIndex, int id) {
		FlowView item = new FlowView(this);
		// item.setPadding(3, 1, 5, 4);
		item.setRowIndex(rowIndex);
		item.setId(id);
		item.setViewHandler(handler);
		item.setFileName(filename);
		item.setItemWidth(item_width);
		item.LoadImage();

		Intent it = new Intent(MyActivity.this, DetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putInt("id", id);
		it.putExtras(bundle);
		item.setClickIntent(it);
	}

	@Override
	public void onBottom() {
		AddItemToContainer(++(waterfall_scroll.current_page),
				waterfall_scroll.pageCount);
	}

	@Override
	public void onTop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAutoScroll(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub

	}
}
